﻿using System.Collections.Generic;
using System.Linq;

namespace OpenWaves.ActivityFeed.NHibernate
{
    public static class SubscriberExtensions
    {
        public static IQueryable<Subscriber> WhoFollowAnyOfTopics(this IQueryable<Subscriber> subscribers, IEnumerable<Topic> topics)
        {
            var topicsNames = topics.Select(t => t.Name).ToList();
            return subscribers.Where(s => s.Subscriptions.Any(t => topicsNames.Contains(t.Name)));
        }

        public static IQueryable<Subscriber> WhoFollowAnyOfTopics(this IQueryable<Subscriber> subscribers, params Topic[] topics)
        {
            var topicsNames = topics.Select(t => t.Name).ToList();
            return subscribers.Where(s => s.Subscriptions.Any(t => topicsNames.Contains(t.Name)));
        }        

        public static IQueryable<Subscriber> WithAtLeastOneSubscription(this IQueryable<Subscriber> query)
        {
            return query.Where(subscriber => subscriber.Subscriptions.Any());
        }

        public static IQueryable<Subscriber> WithIds(this IQueryable<Subscriber> query, IEnumerable<string> subscribersIds)
        {
            return query.Where(subscriber => subscribersIds.Contains(subscriber.Id));
        }
    }
}